#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>

#define PARSEDIR  "./parsedir"
#define CHANNEL   8
#define SAMPLEBIT 32

struct wav_header {
	uint32_t riff_id;
	uint32_t riff_sz;
	uint32_t riff_fmt;
	uint32_t fmt_id;
	uint32_t fmt_sz;
	uint16_t audio_format;
	uint16_t num_channels;
	uint32_t sample_rate;
	uint32_t byte_rate;
	uint16_t block_align;
	uint16_t bits_per_sample;
	uint32_t data_id;
	uint32_t data_sz;
};

int main(int argc, char **argv)
{
	char *wavfile = NULL;
	FILE *fp, *pcmfp[8];
	char *buffer, *offset;
	char pcmname[32];
	int i, ret = -1, n;

	if (argc < 2) {
		printf("argc is error\n");
		return -1;
	}

	wavfile = argv[1];
	fp = fopen(wavfile, "rb+");
	if (!fp) {
		printf("open %s failed\n", wavfile);
		return -1;
	}

	//prepare 8 pcm file
	for (i = 0; i < 8; i++) {
		sprintf(pcmname, "%s/%d.pcm", PARSEDIR, i);
		pcmfp[i] = fopen(pcmname, "wb+");
		if (!pcmfp[i]) {
			printf("open %s failed\n", pcmname);
			return -1;
		}
	}

	buffer = (char *)malloc(sizeof(char)*CHANNEL*SAMPLEBIT/8);
	printf("int %ld, uint32_t %ld head %ld\n", sizeof(unsigned int), sizeof(uint32_t), sizeof(struct wav_header));
	fseek(fp, sizeof(struct wav_header), SEEK_SET);	
	while ((ret = fread(buffer, 1, sizeof(char)*SAMPLEBIT/8, fp)) > 0) {
		fwrite(buffer, 1, ret, pcmfp[n]);
		n++;
		n %= 8;
	}

	fclose(fp);
	return 0;
}
